<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Validate;
use think\Session;
use app\admin\controller\Base;


class Node extends Base
{
    public function index()
    {
        $list = Db::name('node')
            ->field(['id', 'name','mname','aname','status'])
            ->order('id', 'ASC')
            ->paginate(5);
       

       return view('/node/index', [
            'title' => '权限管理',
            'content'=>'权限列表',
            'list' => $list
        ]);
        
    }

    public function add()
    {
        return view('node/add',[
            'title' => '权限管理',
            'content' => '添加权限'
            ]);
    }

    public function doadd()
    {
        $data = input('post.');
        
        $rule =['name'  => 'require|regex:/^[\x{4e00}-\x{9fa5}]{2,26}$/u',
                'mname'   => 'require|regex:/^[a-zA-Z_]{2,20}$/',
                'aname' => 'require|regex:/^[a-zA-Z_]{2,20}$/',
                'status'=>'require|boolean'
                ];

        $message  =[
            'name.require' => '权限名必须填',
            'name.regex'     => '权限名为2-26位中文汉字',
            'mname.require'   => '控制器名必须填',
            'mname.regex'=>'控制器名为2-20位英文字母',
            'mname.require'   => '方法名必须填',
            'mname.regex'=>'方法名为2-20位英文字母',
            'status.require'  => '确认密码必须填',
            'status.boolean' =>'状态为启用或者禁用'
            ];

        $validate = new Validate($rule,$message);
        $result   = $validate->check($data);


        if($result){
            
            $res =  db('node')->insertGetId($data);

            $this->success('新增成功', '/admin/Node');
        }else{
            $this->error('新增失败','/admin/Nadd');
        }
    }


    public function delete($id='')
    {
        Db::startTrans();
        try{
            
            Db::name('role_node')->where('nid',$id)->delete();
            Db::name('node')->where('id',$id)->delete();
            
            Db::commit();
            return 200;
        } catch (Exception $e) { 
            Db::rollback();
            return false;
        }  
    }
}
